using System;
using System.Web;
using DataBuddy;

namespace Astro.Core
{
	public class PostPage : TemplatedThemePage
	{
		private string viewName;
		protected override string ViewName
		{
			get
			{
				return ViewLookUp(".ascx", "post.ascx");
			}
		}

		protected override string ViewLookUp(string baseName, string defaultViewName)
		{
			Category category = new CategoryController().GetCachedCategory(CategoryID, false);

			if (ViewExists(CategoryName + "." + PostName + baseName))
				return CategoryName + "." + PostName + baseName;
			else if (ViewExists(CategoryName.Replace("/", ".") + ".post" + baseName))
				return CategoryName.Replace("/", ".") + ".post" + baseName;
			else if (ViewExists(CategoryName + ".post" + baseName))
				return CategoryName + ".post" + baseName;
			else if (defaultViewName == "post.ascx" && ViewExists(PostName + ".ascx"))
				return PostName + ".ascx";
			else if (ViewExists(PostName + baseName))
				return PostName + baseName;

			// Subcategories
			if (category.ParentId > 0)
			{
				// parent-name.child-name.ascx
				if (ViewExists(category.LinkName.Replace("/", ".") + ".post" + baseName))
					return category.LinkName.Replace("/", ".") + ".post" + baseName;

				// childcategory.parent-name.post.ascx
				if (ViewExists("childcategory." + category.Parent.LinkName + ".post" + baseName))
					return "childcategory." + category.Parent.LinkName + ".post" + baseName;

                if (ViewExists(category.LinkName.Replace("/", ".") + "." + PostName + baseName))
                    return category.LinkName.Replace("/", ".") + "." + PostName + baseName;

                if (ViewExists(category.LinkName.Replace("/", ".") + baseName))
                    return category.LinkName.Replace("/", ".") + baseName;

                if (ViewExists(category.Parent.LinkName + ".post" + baseName))
                    return category.Parent.LinkName + ".post" + baseName;

                if (ViewExists(category.Parent.LinkName + baseName))
                    return category.Parent.LinkName + baseName;
			}
			// return CategoryName.layout.ascx for child posts, but not CategoryName.ascx
			else if (ViewExists(CategoryName + baseName) && baseName != ".ascx")
				return CategoryName + baseName;

			else if (CategoryID == CategoryController.UnCategorizedId && ViewExists("page" + baseName))
				return "page" + baseName;

			else if (ViewExists("post" + baseName))
				return "post" + baseName;

			else if (ViewExists(defaultViewName))
				return defaultViewName;

			return base.ViewLookUp(baseName, defaultViewName);
		}

		protected override void LoadContent(AstroContext astroContext)
		{

			astroContext["where"] = "post";

			Post post = Post.GetCachedPost(PostId);

            if (!post.Category.IsRouting)
            {
                throw new Exception("Not routing for this category");
            }

			if (post.IsDeleted || (!post.IsPublished && AstroUsers.Current == null))
			{
				RedirectTo(new Urls().Home);
			}
			else if (PostName != null && CategoryName != null && (!Util.AreEqualIgnoreCase(PostName, post.Name) || !Util.AreEqualIgnoreCase(CategoryName, post.Category.LinkName)))
			{
				RedirectTo(post.Url);
			}

            if (SiteSettings.Get().EnableReporting)
            {
                if (Context.Request.Cookies["Astro-Post-" + PostId] == null)
                {
                    Post.UpdateViewCount(PostId);
                    HttpCookie cookie = new HttpCookie("Astro-Post-" + PostId, PostId.ToString());
                    Context.Response.Cookies.Add(cookie);
                }
            }

			astroContext["title"] = post.Title + " : " + SiteSettings.Get().Title;

			astroContext["category"] = post.Category;

			astroContext["post"] = post;

			astroContext.RegisterOnRequestDelegate("feedback", GetPostFeedback);
			astroContext.RegisterOnRequestDelegate("comments", GetPostComments);
			astroContext.RegisterOnRequestDelegate("trackbacks", GetPostTrackbacks);
		}

		protected virtual object GetPostFeedback(string key, AstroContext astroContext)
		{
			return new Data().PostFeedback(PostId);
		}

		protected virtual object GetPostComments(string key, AstroContext astroContext)
		{
			return new Data().PostComments(PostId);
		}

		protected virtual object GetPostTrackbacks(string key, AstroContext astroContext)
		{
			return new Data().PostTrackbacks(PostId);
		}
	}
}